import os
import time
import schedule
from datetime import datetime
from build import BuildFlashHandler
from execute import RunTestsHandler
from flask import Flask, render_template, make_response, request
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger

scheduler = BackgroundScheduler()
CODE_PATH = "/home/xxxx/xxxx/openharmony"

# 关键任务
def task():
    update_code_time = datetime.now().strftime("%Y-%m-%d-%H")
    build_flash_handler = BuildFlashHandler(CODE_PATH)
    build_flash_handler.process()
    time.sleep(30)
    run_tests_handler = RunTestsHandler(CODE_PATH, update_code_time)
    run_tests_handler.process()

scheduler.add_job(task, CronTrigger.from_crontab('0 12 * * *'))
scheduler.add_job(task, CronTrigger.from_crontab('0 0 * * *'))
scheduler.start()


try:
    while True:
        time.sleep(1)
except (KeyboardInterrupt, SystemExit):
    scheduler.shutdown()
